package com.motus.pm.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.motus.pm.dao.DeptDao;
import com.motus.pm.dao.DeptTaskDao;
import com.motus.pm.dao.EmpInfoDao;
import com.motus.pm.dao.MeetingInfoDao;
import com.motus.pm.dao.MeetingEmpDao;
import com.motus.pm.dao.ProjectInfoDao;
import com.motus.pm.service.MeetingService;
import com.motus.pm.vo.MotusDept;
import com.motus.pm.vo.MotusEmpInfo;
import com.motus.pm.vo.PmDeptTask;
import com.motus.pm.vo.PmMeetingEmp;
import com.motus.pm.vo.PmMeetingEmpId;
import com.motus.pm.vo.PmMeetingInfo;
import com.motus.pm.vo.PmProjectInfo;
import com.motus.pm.vo.modelDriven.NewMeetingInfo;

@Service
@Transactional
public class MeetingServiceImpl implements MeetingService {

	/**
	 * 根据会议类型进行会议存储
	 */
	@Autowired
	@Qualifier("meetingInfoDaoImpl")
	private MeetingInfoDao meetingInfoDao;
	@Autowired
	@Qualifier("projectInfoDaoImpl")
	private ProjectInfoDao projectDao;
	@Autowired
	@Qualifier("empInfoDaoImpl")
	private EmpInfoDao empDao;
	@Autowired
	@Qualifier("deptDaoImpl")
	private DeptDao deptDao;
	@Autowired
	@Qualifier("meetingEmpDaoImpl")
	private MeetingEmpDao meetingEmpDao;
	@Autowired
	@Qualifier("deptTaskDaoImpl")
	private DeptTaskDao deptTaskDao;

	@Override
	public void newMeeting(NewMeetingInfo meeting) {
		//新建会议
		PmMeetingInfo meeting_temp = new PmMeetingInfo(meeting.getMname(), meeting.getMeetingDate(),meeting.getMeetingContent(),meeting.getMeetingType());
		meetingInfoDao.save(meeting_temp);
		//添加参会人员
		for(int i=0;i<meeting.getMeetingEmps().length;i++){
				MotusEmpInfo empInfo = new MotusEmpInfo();
				PmMeetingEmpId empId = new PmMeetingEmpId();
				PmMeetingEmp meetingEmp = new PmMeetingEmp(); 
				empInfo = empDao.getById(meeting.getMeetingEmps()[i]);
				empId.setMotusEmpInfo(empInfo);
				empId.setPmMeetingInfo(meeting_temp);
				meetingEmp.setId(empId);
				meetingEmpDao.save(meetingEmp);
		}
		PmDeptTask deptTask = new PmDeptTask();
		deptTask.setPmMeetingInfo(meeting_temp);
		MotusDept dept = new MotusDept();
		MotusDept company = deptDao.getById(1);//获取总经办部门信息
		//添加任务信息
		switch (meeting.getMeetingType()) {
		case 1: //公司例会		
				//添加多项任务
			if(meeting.getCommonMeetingTaskDept() != null){
				for(int j=0;j<meeting.getCommonMeetingTaskName().length;j++){
						deptTask.setDeptTaskContent(meeting.getCommonMeetingTaskComment()[j]);//任务内容
						deptTask.setDeptTaskName(meeting.getCommonMeetingTaskName()[j]);    //任务名称
						dept = deptDao.getById(meeting.getCommonMeetingTaskDept()[j]);
						deptTask.setDeptTaskPlanStartTime(meeting.getCommonMeetingTaskStartTime()[j]);
						deptTask.setDeptTaskPlanEndTime(meeting.getCommonMeetingTaskEndTime()[j]);
						deptTask.setDeptTaskPoint(meeting.getCommonMeetingTaskPoint()[j]);
						deptTask.setMotusDept(dept);
 						deptTaskDao.save(deptTask);//存储部门任务
						company.setPoint(company.getPoint() - meeting.getCommonMeetingTaskPoint()[j]); //总经办分数减轻
						deptDao.update(company);
				}
			}
				 break;
		case 2:
			//立项会议
			 PmProjectInfo project_temp = new PmProjectInfo(meeting.getPid(),meeting.getPname(), meeting.getPpoint());
			 projectDao.save(project_temp);
			 break;
		case 3://讨论会
			for(int k=0;k<meeting.getDiscussMeetingTaskName().length;k++){
				deptTask.setDeptTaskContent(meeting.getDiscussMeetingTaskComment()[k]);//任务内容
				deptTask.setDeptTaskName(meeting.getDiscussMeetingTaskName()[k]);    //任务名称
				dept = deptDao.getById(meeting.getDiscussMeetingTaskDept()[k]);
				deptTask.setDeptTaskPlanStartTime(meeting.getDiscussMeetingTaskStartTime()[k]);
				deptTask.setDeptTaskRealEndTime(meeting.getDiscussMeetingTaskEndTime()[k]);
				deptTask.setDeptTaskRealPoint(meeting.getDiscussMeetingTaskPoint()[k]);
				deptTask.setMotusDept(dept);
				deptTaskDao.save(deptTask);//存储部门任务
				company.setPoint(company.getPoint() - meeting.getDiscussMeetingTaskPoint()[k]); //总经办分数减轻
				deptDao.update(company);         
			}
			  break;
		default:
			break;
		}
	}

	@Override
	public List<MotusEmpInfo> empList(int deptid) {
		// TODO Auto-generated method stub
		return empDao.getEmpInfosByDeptId(deptid);
	}

	@Override
	public List<MotusDept> deptList() {
		// TODO Auto-generated method stub
		return deptDao.deptInfo();
	}

	@Override
	public void saveMeetingEmp(PmMeetingInfo meeting, String[] empname) {
		// TODO Auto-generated method stub
		for (int i = 0; i < empname.length; i++) {
			MotusEmpInfo emp = empDao.getEmpByName(empname[i]);
			PmMeetingEmpId meetingEmpId = new PmMeetingEmpId(meeting, emp);
			PmMeetingEmp meetingEmp = new PmMeetingEmp(meetingEmpId);
			meetingEmpDao.save(meetingEmp);
		}
	}

	@Override
	public List<PmMeetingInfo> meetingListByType(int type) {
		// TODO Auto-generated method stub
		// 查看全部会议
		if (type == 0) {
			return meetingInfoDao.getAll();
		}else{
			return meetingInfoDao.getMeetingInfoListByType(type);
		}
}
}
